package com.spdd.mt.biz.manager.service;

import com.spdd.mt.biz.constant.MtResponseStatus;
import com.spdd.mt.biz.constant.ResponseCode;
import com.spdd.mt.biz.exception.NotifyException;
import com.spdd.mt.biz.manager.feishu.FeiShuService;
import com.spdd.mt.biz.manager.model.WebRequest;
import com.spdd.mt.biz.manager.model.WebResponse;
import com.spdd.mt.common.http.HttpUtil;
import com.spdd.mt.common.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MeituanService {

    @Autowired
    private HttpUtil httpUtil;

    @Autowired
    private FeiShuService feiShuService;

    /**
     * 抖店http请求
     *
     * @param notifyRequest 请求响应对象泛型类型
     * @param url           回调请求地址
     */
    @Retryable(value = {
            NotifyException.class
    },
            maxAttempts = 3, // 重试3次
            backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 5000) // 1秒 2秒 和 4秒 会进行重试
    )
    public <T extends WebRequest> boolean sendMtNotify(T notifyRequest, String url) {
        log.info("回调请求方，请求参数:{}, url:{}", JacksonUtils.toJson(notifyRequest), url);

        try {
            // 请求美团回调接口
            WebResponse response = httpUtil.post(url, notifyRequest, new ParameterizedTypeReference<>() {
            });

            log.info("回调响应:{}", JacksonUtils.toJson(response));
            if (MtResponseStatus.SUCCESS.getValue().equalsIgnoreCase(response.getStatus())) {
                log.info("回调成功");
                return true;
            }

            // 非SUCCESS抛出异常，进行重试
            throw new NotifyException(ResponseCode.NOTIFY_ERROR);
        } catch (Exception e) {
            feiShuService.sendExceptionMsg(e);
            throw new NotifyException(ResponseCode.INNER_ERROR, "通知异常，error=" + e.getMessage());
        }
    }

    // public boolean sendDyNotify(TopupResultParam resultParam) {
    //
    //     AccessToken accessToken = appConfig.getDySdkAccessToken(appConfig.getShopId());
    //     log.info("accessToken:{}", JacksonUtils.toJson(accessToken));
    //
    //     TopupResultRequest resultRequest = new TopupResultRequest();
    //     resultRequest.setParam(resultParam);
    //
    //     log.info("回调请求方，请求参数:{}", resultRequest);
    //
    //     try {
    //         TopupResultResponse response = resultRequest.execute(accessToken);
    //
    //         log.info("回调请求方响应，响应参数:{}", JacksonUtils.toJson(response));
    //
    //         // 响应成功
    //         if (DyOrderStatus.SUCCESS.equals(response.getData().getResult())) {
    //             return true;
    //         }
    //
    //         // 非SUCCESS抛出异常，进行重试
    //         throw new ApiException(DyErrorCode.NOTIFY_ERROR);
    //     } catch (Exception e) {
    //         feiShuService.sendExceptionMsg(e);
    //
    //         throw new ApiException(DyErrorCode.INNER_ERROR, "通知异常，error = " + e.getMessage());
    //     }
    // }
}
